package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void insertAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insertAddressBook(addressBook);
    }

    @Override
    public List<AddressBook> list(AddressBook addressBook) {
        log.info("查询当前用户地址信息");
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> list=addressBookMapper.list(userId);
        return list;
    }

    @Override
    public void setDefaul(AddressBook addressBook) {
        log.info("设置默认地址");
        //把所有地址修改为非默认地址
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        addressBookMapper.updateIsDefalutByUserId(addressBook);
        //设置当前地址为默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        log.info("根据id查询地址信息");
        AddressBook addressBook = addressBookMapper.getById(id);
        return addressBook;
    }

    @Override
    public void update(AddressBook addressBook) {
        log.info("根据id修改地址信息");
        addressBookMapper.update(addressBook);
    }

    @Override
    public void delete(Long id) {
        log.info("根据id删除地址信息");
        addressBookMapper.delete(id);
    }

}
